package com.example.wan1.adapter;

import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wan1.R;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.utils.ToolsUtils;

import java.util.List;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/29 17:03
 * @Description:
 */
public class ArticleListAdapter extends BaseQuickAdapter<ArticleBean, BaseViewHolder> {

    public ArticleListAdapter(int layoutResId, @Nullable List<ArticleBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ArticleBean item) {
        //分类
        helper.setText(R.id.article_tv_category, item.getSuperChapterName() + " | " + item.getChapterName());
        //分享人
        if (!TextUtils.isEmpty(item.getShareUser()))
            helper.setText(R.id.article_tv_author, item.getShareUser());
        else
            helper.setText(R.id.article_tv_author, item.getAuthor());
        //时间+标题
        helper.setText(R.id.article_tv_time, item.getNiceDate());
        helper.setText(R.id.article_tv_title, Html.fromHtml(item.getTitle()).toString());
        //收藏
        if (item.isCollect()) {
            helper.setImageResource(R.id.article_iv_collect, R.mipmap.heart_red);
        } else {
            helper.setImageResource(R.id.article_iv_collect, R.mipmap.heart_gray);
        }
        //新
        if (item.isFresh()) {
            helper.getView(R.id.article_tv_new).setVisibility(View.VISIBLE);
        } else
            helper.getView(R.id.article_tv_new).setVisibility(View.GONE);

        //问答、公众号
        List<ArticleBean.TagsBean> tags = item.getTags();
        if (tags.size() != 0) {
            if (tags.size() == 3) {
                helper.getView(R.id.article_tv_question).setVisibility(View.VISIBLE);
                helper.getView(R.id.article_tv_gzh).setVisibility(View.VISIBLE);
                helper.getView(R.id.article_tv_web).setVisibility(View.VISIBLE);
            } else {
                for (int i = 0; i < tags.size(); i++) {
                    if ("问答".equals(tags.get(i).getName()))
                        helper.getView(R.id.article_tv_question).setVisibility(View.VISIBLE);
                    else if ("公众号".equals(tags.get(i).getName()))
                        helper.getView(R.id.article_tv_gzh).setVisibility(View.VISIBLE);
                    else if ("本站发布".equals(tags.get(i).getName()))
                        helper.getView(R.id.article_tv_web).setVisibility(View.VISIBLE);
                }
            }
        } else {
            helper.getView(R.id.article_tv_question).setVisibility(View.GONE);
            helper.getView(R.id.article_tv_gzh).setVisibility(View.GONE);
            helper.getView(R.id.article_tv_web).setVisibility(View.GONE);
        }

        //置顶是另外一个url
        if (item.getType() == 1) {
            helper.getView(R.id.article_tv_top).setVisibility(View.VISIBLE);
        } else
            helper.getView(R.id.article_tv_top).setVisibility(View.GONE);

        // TODO: 2020/2/12 爱心、分类的点击
        helper.addOnClickListener(R.id.article_iv_collect);
        helper.addOnClickListener(R.id.article_tv_author);
//        helper.addOnClickListener(R.id.home_contentlist_iv)
//                .addOnClickListener(R.id.)

        ConstraintLayout cl = helper.getView(R.id.article_cl);
        GradientDrawable background = (GradientDrawable) cl.getBackground();
        background.setStroke(2, ToolsUtils.getColor(mContext));
        cl.setBackground(background);
    }
}
